package com.view;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.UIManager;
import javax.swing.plaf.RootPaneUI;

import org.jb2011.lnf.beautyeye.BeautyEyeLNFHelper;

public class Start {

	private JFrame frame;
	private JLabel jLabel;
	private JTextArea area;
	private JScrollPane jScrollPane;
	private JButton button;
	private JPanel jPanel;
	private JPanel jPanel2;
	private Font font;
	
	public Start() {
		super();
		frame=new JFrame("迎接我们的幸运者");
		frame.setSize(600, 500);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		font=new Font("楷体",Font.BOLD,30);
		
		jLabel=new JLabel("请输入参与抽奖的用户，每行一个",JLabel.CENTER);
		
		jLabel.setFont(font);
		
		jLabel.setForeground(Color.white);
		
		area=new JTextArea(10, 10);
		
		jScrollPane=new JScrollPane(area);
		
		button=new JButton("进入抽奖环节");
		
		button.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				//创建数组
				String[] str=area.getText().split("\n");
				
				//保存配置文件
				write();
				
				new App(str);
			}
		});
	
		
		frame.setLocationRelativeTo(null);
		
		frame.setLayout(new BorderLayout());
		
		frame.add(BorderLayout.NORTH,jLabel);
		
	
		
		frame.add(BorderLayout.CENTER,jScrollPane);
		
		frame.add(BorderLayout.SOUTH,button);
		
		frame.getContentPane().setForeground(Color.white);
		frame.getContentPane().setBackground(Color.red);
		
		frame.setVisible(true);
		
		//读取数据
		read();
	}
	
	public void read(){
		String path=Start.class.getClassLoader().getResource("").getPath();
		File file=new File(path+"config.ini");
		if(file.exists()){
			BufferedReader bufferedReader=null;
			try {
				 bufferedReader=new BufferedReader(new FileReader(file));
				 String str="";
				 while((str=bufferedReader.readLine())!=null){
					 area.append(str+"\n");
				 }
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			finally{
				try {
					bufferedReader.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}

	public void write(){
		String path=Start.class.getClassLoader().getResource("").getPath();
		File file=new File(path+"config.ini");
		if(!file.exists()){
			try {
				file.createNewFile();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		BufferedWriter bufferedWriter=null;
		
		try {
			bufferedWriter=new BufferedWriter(new FileWriter(file));
			bufferedWriter.write(area.getText());
		} catch (Exception e) {
			// TODO: handle exception
		}
		finally{
			try {
				bufferedWriter.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
	}

	public static void main(String[] args) {
		 try
		    {
		        BeautyEyeLNFHelper.frameBorderStyle=BeautyEyeLNFHelper.FrameBorderStyle.generalNoTranslucencyShadow;
		        UIManager.put("RootPane.setupButtonVisible", false);
		        BeautyEyeLNFHelper.launchBeautyEyeLNF();
		    }
		    catch(Exception e)
		    {
		        //TODO exception
		    }
		
		new Start();
	}

}
